package cn.pw.test.com.conversion.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.pw.test.com.conversion.R;


/**
 * 换算数据输入控件
 */
public class ConversionInputView extends LinearLayout {
    private View mView;

    private TextView termTx;
    private TextView unitTx;
    private EditText inputEd;

    public ConversionInputView(Context context) {
        super(context);
        init(context, null);
    }

    public ConversionInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ConversionInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mView = LayoutInflater.from(context)
                .inflate(R.layout.conversion_layout_conversion_input_view, this, true);
        termTx = mView.findViewById(R.id.conversion_input_view_term_tx);
        unitTx = mView.findViewById(R.id.conversion_input_view_unit_tx);
        inputEd = mView.findViewById(R.id.conversion_input_view_input_et);

        if (attrs == null)
            return;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ConversionInputView);
        String term = ta.getString(R.styleable.ConversionInputView_ciTermText);
        String unit = ta.getString(R.styleable.ConversionInputView_ciUnitText);
        String input = ta.getString(R.styleable.ConversionInputView_ciInputText);
        String hint = ta.getString(R.styleable.ConversionInputView_ciInputHint);

        termTx.setText(term);
        unitTx.setText(unit);
        if (input == null || input.isEmpty()) {
            inputEd.setHint(hint);
        } else {
            inputEd.setText(input);
        }
    }

    public TextView getTermTx() {
        return termTx;
    }

    public TextView getUnitTx() {
        return unitTx;
    }

    public EditText getInputEd() {
        return inputEd;
    }
}
